/*
 * Font.java
 *
 * Created on November 9, 2006, 11:40 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package edu.umn.cs5115.scheduler.framework.gui;

import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.GC;

import edu.umn.cs5115.scheduler.framework.Application;
import edu.umn.cs5115.scheduler.framework.DocumentController;

/**
 * This class creates an SWT font when needed, but can be initialized without
 * actually creating the font.  This is useful for initializing static members,
 * before a display may have actually been created.  It must be around the first
 * time the font is actually used.
 * @author grant
 */
public class Font
{
    /** Actual SWT Font for this Font. */
    private org.eclipse.swt.graphics.Font swtFont;
    /** Name of the font, (for example, "Helvetica", or "Lucida Grande") */
    private String name;
    /** Size of the font (12 pt, etc.) */
    private int height;
    /** Style constants for the font, as defined by SWT. */
    private int style;
    
    /**
     * Creates a new instance of Font that will use the system label font.
     */
    public Font()
    {
        name = null;
        height = -1;
        style = -1;
    }
    
    /**
     * Creates a new instance of Font
     * @param name The name of the font.  If this is null, the system label font 
     * will be used.
     * @param height The size of the font, in points.
     * @param style The SWT style constants to use when creating the font, any
     * combination of SWT.NORMAL, SWT.BOLD and SWT.ITALIC
     */
    public Font(String name, int height, int style)
    {
        this.name = name;
        this.height = height;
        this.style = style;
    }
    
    /**
     * Configure a GC to use this font.
     * @param gc The gc to configure to use this font.
     */
    public void setFont(GC gc)
    {
        if (swtFont == null)
        {
            if (name == null || height == -1 || style == -1)
            {
                FontData data = Application.getSystemLabelFont().getFontData()[0];
                if (name == null)
                    name = data.getName();
                if (height == -1)
                    height = data.getHeight();
                if (style == -1)
                    style = data.getStyle();
            }
            swtFont = new org.eclipse.swt.graphics.Font(DocumentController.getSharedDocumentController().getDisplay(), name, height, style);
        }
        gc.setFont(swtFont);
    }
    
    /** Frees the SWT resources associated with this font.  You can call this to 
     * free resources when you are done with them, or wait for it to be called
     * automatically when this Font object gets finalized during garbage collection.
     */
    public void dispose()
    {
        if (swtFont != null)
        {
            swtFont.dispose();
            swtFont = null;
        }
    }
    
    /**
     * Calls dispose, then super.finalize()
     * @throws Throwable if super's implementation does.
     */
    protected void finalize() throws Throwable
    {
        dispose();
        super.finalize();
    }
}
